<template>
  <el-row style="width:100%">
    <el-col :span="20" :class="{ bold: treeData.isRoot }">
      <i class="el-icon-s-home" v-if="treeData.isRoot"></i>
      <i class="el-icon-user-solid" v-else-if="!treeData.children"></i
      >{{ treeData.name }}</el-col
    >
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeData.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from "@/api/departments";
export default {
  name: "departments",
  props: {
    treeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case "add":
          // 添加部门
          this.$emit("addDept", this.treeData.id);
          break;
        case "edit":
          // 修改部门
          this.$emit("editDept", this.treeData.id);
          break;
        case "del":
          // 删除部门
          this.$confirm("此操作将永久删除该文件, 是否继续?", "温馨提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(async () => {
              await delDepartments(this.treeData.id);
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.$emit("delDept");
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除"
              });
            });
          break;
      }
    }
  }
};
</script>

<style lang="scss" scope>
.bold {
  font-weight: 700;
}
.el-divider {
  margin: 12px 0;
}
</style>
